<?php

namespace App\Http\Controllers\Admin;

use App\Models\Shop;
use App\Models\ShopAuth;
use App\Traits\Mall\ShopManagerTrait;
use Illuminate\Http\Request;

class ShopController extends BaseController
{
    use ShopManagerTrait;

    /**
     * @return bool
     */
    protected function authenticate()
    {
        return false;
    }

    /**
     * 店铺列表
     * @throws \Exception
     */
    public function index(Request $request)
    {
        return $this->showShops($request);
    }

    /**
     * @param Request $request
     * @param \Illuminate\Contracts\Pagination\Paginator $shops
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showShopsView(Request $request, $shops)
    {
        $this->assign(array_merge([
            'tab' => 'all',
            'shop_name' => '',
            'username' => '',
            'phone' => '',
            'shop_state' => '',
            'time_begin' => '',
            'time_end' => ''
        ], $request->all()));
        return $this->view('admin.shop.shops', [
            'shops' => $shops,
            'pagination' => $shops->appends($request->except('page'))->render()
        ]);
    }


    /**
     * 等待审核
     * @throws \Exception
     */
    public function pending()
    {

        if ($this->isOnSubmit()) {
            $shops = $this->request->input('shops');
            $eventType = $this->request->input('eventType');

            if ($eventType === 'delete') {
                foreach ($shops as $shop_id) {
                    ShopAuth::where('shop_id', $shop_id)->delete();
                }
            }

            if ($eventType === 'accept') {
                foreach ($shops as $shop_id) {
                    ShopAuth::where('shop_id', $shop_id)->update(['auth_status' => 1]);
                    Shop::withoutGlobalScopes()->where('shop_id', $shop_id)->update(['auth_status' => 1]);
                }
            }

            if ($eventType === 'refuse') {
                foreach ($shops as $shop_id) {
                    ShopAuth::where('shop_id', $shop_id)->update(['auth_status' => 0]);
                    Shop::withoutGlobalScopes()->where('shop_id', $shop_id)->update(['auth_status' => 0]);
                }
            }

            return ajaxReturn();

        } else {

            $authlist = ShopAuth::with('shop.user')->whereHas('shop')->where('auth_state', '0')->paginate(20);
            $this->assign([
                'pagination' => $authlist->render(),
                'shoplist' => $authlist->map(function ($auth) {
                    return $auth->shop;
                })
            ]);

            return $this->view('admin.shop.pending');
        }
    }

    /**
     * 店铺详情
     */
    public function detail($shop_id)
    {
        $this->assign([
            'shop_id' => $shop_id,
            'shop' => Shop::withoutGlobalScopes()->find($shop_id),
            'auth' => ShopAuth::where('shop_id', $shop_id)->first()
        ]);

        return $this->view('admin.shop.detail');
    }

    /**
     * 认证店铺
     */
    public function auth()
    {
        $shop_id = $this->request->input('shop_id');
        $auth_status = intval($this->request->input('auth_status'));
        $message = htmlspecialchars($this->request->input('message'));


        Shop::where('shop_id', $shop_id)->update(['auth_status' => $auth_status]);
        ShopAuth::where('shop_id', $shop_id)->update(['auth_status' => $auth_status, 'updated_at' => time()]);
        return $this->messager()->render();
    }
}
